/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Pagos.Formularios;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author FCCCRTI05
 */
public class CambiarPassword extends HttpServlet
{

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    {
        PrintWriter out = response.getWriter();
        //REINICIO DEL BUFFER
        response.reset();
        response.resetBuffer();
        response.setContentType("text/html;charset=ISO-8859-1");
        try
        {
            //CONEXION A LA BD
            db.Conexion conectar = (db.Conexion)request.getSession().getAttribute("db");
            db.CodificadorDecodificador codificador = (db.CodificadorDecodificador)request.getSession().getAttribute("codificador");
            Connection conex = conectar.ObtenerConexionSQLServer();
            //PARAMETROS
            String usuario = conectar.ObtenerUsuario();
            String nueva = codificador.encrypt(request.getParameter("nueva"));
            //GUARDAR DATOS EN LA BD
            PreparedStatement pst = conex.prepareStatement("update F55_PAGOS_USUARIOS"+
            " set password_usu = "+"'"+nueva+"'"+",cambio_pass_usu = 'No'"+
            " where usuario_usu like "+"'"+usuario+"'");
            pst.executeUpdate();
            pst.close();
            //BITACORA
            conectar.GuardarEnBitacora("Cambió contraseña de acceso al sistema");
            //SALIDAS EN HTML
            out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
            out.write("<html xmlns=\"http://www.w3.org/1999/xhtml\">"+"\n");
            out.write("<head>"+"\n");
            out.write("<title></title>"+"\n");
            out.write("<script language=\"javascript\" type=\"text/javascript\">"+"\n"+
            "<!--"+"\n"+
            "window.location=\"/jde/Pagos/?cambio_password=ok\""+"\n"+
            "-->"+"\n"+
            "</script>");
            out.write("</head>"+"\n");
            out.write("<body>"+"\n");
            out.write("</body>"+"\n");
            out.write("</html>"+"\n");
        }
        catch (Exception x)
        {
            //SALIDAS EN HTML
            out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
            out.write("<html xmlns=\"http://www.w3.org/1999/xhtml\">"+"\n");
            out.write("<head>"+"\n");
            out.write("<title>Sistema de notificaci&oacute;n de Pagos a Proveedores - FCC Construcci&oacute;n de CA, S.A</title>"+"\n");
            out.write("<!--MENU-->"+"\n");
            out.write("<link rel=\"stylesheet\" href=\"/jde/scripts/menu/menu-bar.css\" media=\"screen\" type=\"text/css\">"+"\n");
            out.write("<script type=\"text/javascript\" src=\"/jde/scripts/menu/menu-for-applications.js\"></script>"+"\n");
            out.write("<link rel=\"stylesheet\" href=\"/jde/scripts/menu/menu-item.css\" media=\"screen\" type=\"text/css\">"+"\n");
            out.write("<script language=\"javascript\" type=\"text/javascript\">"+"\n");
            out.write("window.onload = function()"+"\n");
            out.write("{"+"\n");
            out.write("Reloj();"+"\n");
            out.write("var menuModel = new DHTMLSuite.menuModel();"+"\n");
            out.write("menuModel.addItemsFromMarkup('menuModel');"+"\n");
            out.write("menuModel.setMainMenuGroupWidth(00);"+"\n");
            out.write("menuModel.init();"+"\n");
            out.write("var menuBar = new DHTMLSuite.menuBar();"+"\n");
            out.write("menuBar.addMenuItems(menuModel);"+"\n");
            out.write("menuBar.setTarget('menu');"+"\n");
            out.write("menuBar.init();"+"\n");
            out.write("};"+"\n");
            out.write("function Reloj()"+"\n");
            out.write("{"+"\n");
            out.write("var fecha = new Date ();"+"\n");
            out.write("var hora = fecha.getHours ( );"+"\n");
            out.write("var minuto = fecha.getMinutes ( );"+"\n");
            out.write("var segundo = fecha.getSeconds ( );"+"\n");
            out.write("minuto = (minuto < 10 ? \"0\" : \"\") + minuto;"+"\n");
            out.write("segundo = (segundo < 10 ? \"0\" : \"\") + segundo;"+"\n");
            out.write("var tiempo = (hora < 12 ) ? \"A.M\" : \"P.M\";"+"\n");
            out.write("hora = (hora > 12) ? hora - 12 : hora;"+"\n");
            out.write("hora = (hora == 0) ? 12 : hora;"+"\n");
            out.write("var cadena_hora = hora + \":\" + minuto + \":\" + segundo + \" \" + tiempo;"+"\n");
            out.write("document.getElementById(\"reloj\").innerHTML = \"<b>\"+cadena_hora+\"</b>\";"+"\n");
            out.write("setTimeout(\"Reloj()\",1000);"+"\n");
            out.write("}"+"\n");
            out.write("</script>"+"\n");
            out.write("</head>"+"\n");
            out.write("<body style=\"font-family:Verdana, Geneva, sans-serif; font-size:11px;\">"+"\n");
            //INCLUIR TOP.JSP
            String top = "/Pagos/home/top.jsp";
            RequestDispatcher rd_top = getServletConfig().getServletContext().getRequestDispatcher(top);
            rd_top.include(request,response);
            //SEGUIR CON HTML
            out.write("<div style=\"width:100%;\">"+"\n");
            out.write("<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">"+"\n");
            out.write("<tr>"+"\n");
            out.write("<td width=\"100%\" align=\"left\">DATOS DE LA SESI&Oacute;N (ERROR, Hora: <span id=\"reloj\"></span>)</td>"+"\n");
            out.write("</tr>"+"\n");
            out.write("</table>"+"\n");
            out.write("</div>"+"\n");
            out.write("<div id=\"todo\" style=\"background-image:url(/jde/img/logo_fondo.png); height:250px; background-repeat:no-repeat; background-position:center;\">"+"\n");
            out.write("<div style=\"margin-bottom:5px; font-weight:bold; background-color:#99ffcc; border:1px solid #666; padding:4px 4px 4px 3px; text-align:center;\">CAMBIAR CONTRASE&Ntilde;A DE USUARIO</div>"+"\n");
            out.write("<div style=\"margin-bottom:5px; background-color:red; color:white; border:1px solid #666; padding:4px 4px 4px 3px; text-align:center;\"><b>SE PRODUJO UN ERROR EN EL SISTEMA, ES POSIBLE QUE LOS DATOS NO SE GUARDARAN</b></div>"+"\n");
            out.write("<p align=\"left\"><b>Favor enviar al administrador el siguiente mensaje de error:</b><br/><br/>"+new db.Utilidades().ObtenerStringStackTrace(x)+"</p>"+"\n");
            out.write("</div>"+"\n");
            out.write("</body>"+"\n");
            out.write("</html>"+"\n");
        }
        finally
        {
            out.flush();
            out.close();
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    {
        processRequest(request, response);
    }
}